package com.liu.user.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.common.entity.Result;
import com.liu.common.entity.StatusCode;
import com.liu.user.entity.vo.AdminGetUserInfoVO;
import com.liu.user.entity.vo.BlindBoxSaleInfoVO;
import com.liu.user.entity.vo.UserRoleVO;
import com.liu.user.service.AdminService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    /**
     * 管理员查询用户列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/getUserInfoList")
    public Result getUserInfoList(@RequestParam("usernameKeyword")String username, @RequestParam("pageNum")String pageNum, @RequestParam("pageSize")String pageSize){
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<AdminGetUserInfoVO> userInfoList = adminService.getUserInfoList(username);
//        userInfoList.forEach(u->u.setPassword("******"));
        return new Result(true, StatusCode.OK,"查询成功",new PageInfo<>(userInfoList));
    }

    /**
     * 获取用户详细信息
     * @param username
     * @return
     */
//    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
//    @GetMapping("/getUserDetails")
//    public Result getUserDetails(@RequestParam("username")String username){
//        UserDetailsVO userDetails = adminService.getUserDetails(username);
//        return new Result(true, StatusCode.OK,"查询成功",userDetails);
//    }

    /**
     * 根据用户名查询用户
     * @param username
     * @param pageNum
     * @param pageSize
     * @return
     */
//    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
//    @GetMapping("/getUserInfoListByUsername")
//    public Result getUserInfoListByUsername(@RequestParam("username")String username,@RequestParam("pageNum")String pageNum,
//                                            @RequestParam("pageSize")String pageSize){
//        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
//        return new Result(true, StatusCode.OK,"查询成功",new PageInfo<>(adminService.getUserInfoListByUsername(username)));
//    }

    /**
     * 获取所有用户的权限
     * @param username
     * @param pageNum
     * @param pageSize
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/getUserRole")
    public Result getUserRole(@RequestParam("usernameKeyword")String username, @RequestParam("pageNum")String pageNum, @RequestParam("pageSize")String pageSize){
        PageInfo<UserRoleVO> userRoleList = adminService.getUserRoleList(username,pageNum,pageSize);
        return new Result(true, StatusCode.OK,"查询成功",userRoleList);
    }

    /**
     * 查询平台盲盒销量
     * @param pageNum
     * @param pageSize
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/getBlindBoxSaleInfo")
    public Result getBlindBoxSaleInfo(@RequestParam("pageNum")String pageNum, @RequestParam("pageSize")String pageSize,@RequestParam(value = "key", required = false) String keyword){
        PageInfo<BlindBoxSaleInfoVO> blindBoxSaleInfo = adminService.getBlindBoxSaleInfo(keyword,pageNum, pageSize);
        return new Result(true, StatusCode.OK,"查询成功",blindBoxSaleInfo);
    }

    /**
     * 导出平台盲盒销量
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/exportBlindBoxSaleInfo")
    public void exportBlindBoxSaleInfo(@RequestParam(value = "key", required = false) String keyword, HttpServletResponse response) throws IOException {
        adminService.exportBlindBoxSaleInfo(keyword, response);
    }
}
